package cn.edu.controller;

import cn.edu.entity.Emp;
import cn.edu.service.EmpService;
import cn.edu.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Mr.ma
 * @since 2022/11/17 14:45
 **/
@CrossOrigin
@RestController
@RequestMapping("emp")
@Api("接收对职员信息表进行CRUD的请求")
public class EmpController {
    @Resource
    private EmpService empService;

    @PostMapping("insert")
    @ApiOperation("插入职员信息")
    public ResultVO insert(@RequestBody Emp emp){
        int insert = empService.insert(emp);
        if (insert >= 0){
            return new ResultVO(200,null,"添加职员信息成功");
        }
        return new ResultVO(500,null,"服务器异常，添加职员信息失败");
    }

    @ApiOperation("获取职员信息（含分页）")
    @GetMapping("selectAll")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "页码", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示多少条数据", defaultValue = "5", dataType = "int")
    })
    public ResultVO selectAll(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "5") Integer pageSize){
        return new ResultVO(200, empService.selectAll(currentPage,pageSize),"获取职员信息成功");
    }

    @ApiOperation("根据主键查询职员信息")
    @GetMapping("selectById")
    @ApiImplicitParam(name = "id", value = "职员编号", dataType = "int")
    public ResultVO selectById(Integer id){
        Emp emp = empService.selectById(id);
        if (emp != null){
            return new ResultVO(200,emp,"获取职员信息成功");
        }
        return new ResultVO(500,null,"服务器异常，获取职员信息失败");
    }

    @ApiOperation("根据主键更新职员信息")
    @PostMapping("updateById")
    public ResultVO updateById(@RequestBody Emp emp){
        int i = empService.updateById(emp);
        if (i >= 0){
            return new ResultVO(200,null,"修改职员信息成功");
        }
        return new ResultVO(500,null,"服务器异常，修改职员信息失败");
    }

    @ApiOperation("根据主键删除职员信息")
    @GetMapping("deleteById")
    @ApiImplicitParam(name = "id", value = "职员编号", dataType = "int")
    public ResultVO deleteById(Integer id){
        int i = empService.deleteById(id);
        if (i >= 0){
            return new ResultVO(200,null,"删除职员信息成功");
        }
        return new ResultVO(500,null,"服务器异常，删除职员信息失败");
    }
}
